﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyThirty_SeventhWPFDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation widthAnimation = new DoubleAnimation();
            widthAnimation.To = this.Width - 30;
            widthAnimation.From = 100;              //从100开始到this.Width - 30
            widthAnimation.Duration = TimeSpan.FromSeconds(1);//动画时间间隔
            button.BeginAnimation(Button.WidthProperty,widthAnimation);

            button.Width = 10;//无效，优先使用动画设置的属性
            //如果不设置widthAnimation.To，则动画会一直到宽度为10

            DoubleAnimation heightAnimation = new DoubleAnimation();
            heightAnimation.To = this.Height - 100;
            heightAnimation.From = 40;
            heightAnimation.Duration = TimeSpan.FromSeconds(1);
            button.BeginAnimation(Button.HeightProperty, heightAnimation);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation widthAnimation = new DoubleAnimation();
            widthAnimation.To = this.Width - 30;
            widthAnimation.From = 200;              //从100开始到this.Width - 30
            widthAnimation.Duration = TimeSpan.FromSeconds(1);//动画时间间隔
            widthAnimation.AutoReverse = true;      //设置动画翻转，返回到动画前状态（From优先），默认false
            button1.BeginAnimation(Button.WidthProperty, widthAnimation);

            DoubleAnimation heightAnimation = new DoubleAnimation();
            heightAnimation.To = this.Height - 100;
            heightAnimation.From = 200;
            heightAnimation.Duration = TimeSpan.FromSeconds(1);
            heightAnimation.FillBehavior = FillBehavior.Stop;   //立即返回最初的动画前的状态
            button1.BeginAnimation(Button.HeightProperty, heightAnimation);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation widthAnimation = new DoubleAnimation();
            widthAnimation.To = this.Width - 30;
            widthAnimation.From = 200;              //从100开始到this.Width - 30
            widthAnimation.Duration = TimeSpan.FromSeconds(1);//动画时间间隔
            widthAnimation.Completed += aniamtion_Completed;
            button2.BeginAnimation(Button.WidthProperty, widthAnimation);

            DoubleAnimation heightAnimation = new DoubleAnimation();
            heightAnimation.To = this.Height - 100;
            heightAnimation.From = 200;
            heightAnimation.Duration = TimeSpan.FromSeconds(1);
            button2.BeginAnimation(Button.HeightProperty, heightAnimation);
        }

        private void aniamtion_Completed(object sender, EventArgs e)
        {
            button2.BeginAnimation(Button.WidthProperty, null);
            button2.Width = 50;
        }
    }
}
